package sk.uniza.fri.cerviix.server.network.messages.client;

import java.io.IOException;

import sk.uniza.fri.cerviix.server.Game;
import sk.uniza.fri.cerviix.server.game.Player;
import sk.uniza.fri.cerviix.server.game.Worm;
import sk.uniza.fri.cerviix.server.network.Client;
import sk.uniza.fri.cerviix.server.network.ClientMessage;
import sk.uniza.fri.cerviix.server.network.messages.server.ConnectResponse;

public class ConnectMessage extends ClientMessage
{
	private String playerName;
	
	public ConnectMessage(Client client)
	{
		super(client);
	}

	@Override
	public void receive() throws IOException
	{
		playerName = stream.readUTF();
	}
	
	@Override
	public void run(Game game)
	{
		Player player = new Player(client, playerName);
		
		client.attachPlayer(player);
		client.sendMessage(new ConnectResponse(player.getId()));
		
		game.addPlayer(player);
	}
}
